package client;



import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import backend.core.transport.Decoder;
import backend.core.transport.Encoder;
import backend.core.transport.Package;


public class ClientHandler extends ChannelInboundHandlerAdapter {

    private Encoder encoder;
    private Decoder decoder;

    public ClientHandler() {
        this.encoder = Encoder.getInstance();
        this.decoder = Decoder.getInstance();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 连接建立时的处理，发送请求消息给服务器
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        // 处理接收到的数据
        try {
            // 处理接收到的数据
            Package pkg = (Package) msg;
            String data = String.valueOf(pkg.getData());
            String err = String.valueOf(pkg.getErr());
            // 打印接收到的消息
            System.out.println("受到服务端响应的消息: " + data);
            System.out.println("受到服务端响应的消息: " + err);

//            try {
//                byte[] res = client.execute(statStr.getBytes());
//                System.out.println(new String(res));
//            } catch (Exception e) {
//                System.out.println(e.getMessage());
//            }
            System.out.print(":> ");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {

        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        System.out.println("xxxxxxxxxx exceptionCaught");
        // 异常处理
        cause.printStackTrace();
        ctx.close();
    }
}